﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CRMSalesManagement.Manage.FrontPageManagement
{
    public partial class NewsEdit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    var news =CRM.Business.NewList.GetNewById(Convert.ToInt32(Session["newsId"]));
                    if (news != null)
                    {
                        this.txtTitle.Text = news.title;
                        this.txtAuthor.Text = news.author;
                        this.content3.Value = news.content;
                    }
                }
                catch (Exception)
                {
                    //跳转至异常页面
                }
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            this.lblAuthor.Text = "";
            this.lblContent.Text = "";
            this.lblSaveResult.Text = "";
            this.lblTitle.Text = "";
            if (this.txtTitle.Text.Trim() == "")
            {
                this.lblTitle.Text = "标题不能为空";
                this.txtTitle.Focus();
            }
            else
            {
                if (this.txtAuthor.Text.Trim() == "")
                {
                    this.lblAuthor.Text = "供稿人不能为空";
                    this.txtAuthor.Focus();
                }
                else
                {
                    if (this.content3.Value.Trim() == "")
                    {
                        this.lblContent.Text = "内容不能为空";
                        this.content3.Focus();
                    }
                    else
                    {
                        var noticesSouce = new CRM.DataLayer.ContentInfor();

                        noticesSouce.ID = Convert.ToInt32(Session["newsId"]);
                        noticesSouce.title = this.txtTitle.Text;
                        noticesSouce.author = this.txtAuthor.Text;
                        noticesSouce.date = DateTime.Now;
                        noticesSouce.content = this.content3.Value.Trim();
                        noticesSouce.image = setImgUrl();
                        noticesSouce.type = Convert.ToString(Session["type"]);
                        bool modify =CRM.Business.NewList.ModifyNews(noticesSouce);
                        if (!modify)
                        {
                            this.lblSaveResult.Text = "保存失败";
                        }
                        else
                        {
                            this.lblSaveResult.Text = "保存成功";
                            Response.Redirect("ManageNewsList.aspx?type=" + noticesSouce.type);
                        }
                    }
                }
            }
        }

        private string setImgUrl()
        {
            try
            {
                string str = content3.Value.Trim();
                int start = str.IndexOf("src", str.IndexOf("<img", 0) + 3) + 5;
                int end = str.IndexOf("/>", start) - 2;

                return str.Substring(start, end - start);
            }
            catch
            {
                return "";
            }
        }
    }
}